package com.zmj.controller;

import com.zmj.common.Res;
import com.zmj.entity.SignUp;
import com.zmj.entity.User;
import com.zmj.service.UserService;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @RestController 写在Controller层，统一返回JSON格式数据
 * @RequestMapping 接口的映射地址 => http://localhost:8080/api/user
 */
@RestController
@RequestMapping("/api/user")


public class UserController {
@Autowired
    private UserService userService;
    /**
     * 用户登录接口
     *
     * @param user 前端传过来的User数据，前端的属性必须跟Java实体类保持一致 ：
     *             {
     *             "account":"pyhan",
     *             "password":"123456"
     *             }
     * @return
     * @PostMapping 接口映射 前端发起POST请求 基于类上边的拼接 => http://localhost:8080/api/user/login
     * @RequestBody 使用这个注解接收请求中BODY的参数
     */

    @PostMapping("/login")
    public Res login(@RequestBody User user){
        System.out.println(user);
        //调用UserService的登录方法
        User loginUser = userService.login(user);
        //返回null说明账号或密码错误，否则就是成功
        if (loginUser == null){
            return new Res(0,"登陆失败！",null);
        }else {
            return  new Res(1,"登录成功！",loginUser);
        }
    }

    /**
     * 用户注册接口   http://localhost:8080/api/user/register
     *
     * @param user 参数是前端传过来的JSON对象，同上
     * @return
     */
    @PostMapping("/register")
    public Res register(@RequestBody User user){
        User registerUser = userService.register(user);
        if (registerUser == null){
            return new  Res(0,"注册失败",null);
        }else {
            return new  Res(1,"注册成功",user);
        }
    }

    @GetMapping("/token/test")
    public Res token(HttpServletRequest request){
        User user = userService.getUserByToken(request);
        return Res.create(1,"用户token",user);

    }




    @GetMapping("/info")
    public Res getUserInfo(){
        List<User> list = userService.getUserInfo();
        if (list != null){
            return Res.create(1,"报名信息",list);
        }else {
            return Res.create(0,"???",null);
        }
    }



}
